// 合约配置文件
// 部署完成后，请更新对应网络的合约地址

export const NETWORKS = {
  hardhat: {
    name: 'Hardhat Local',
    chainId: 31337,
    rpcUrl: 'http://localhost:8545',
    tag: 'local',
    color: 'blue',
    blockExplorer: 'http://localhost:8545' // 本地无区块浏览器
  },
  sepolia: {
    name: 'Sepolia Testnet', 
    chainId: 11155111,
    rpcUrl: 'https://sepolia.infura.io/v3/YOUR_INFURA_KEY', // 替换为您的Infura Key
    tag: 'testnet',
    color: 'orange',
    blockExplorer: 'https://sepolia.etherscan.io'
  },
  goerli: {
    name: 'Goerli Testnet',
    chainId: 5,
    rpcUrl: 'https://goerli.infura.io/v3/YOUR_INFURA_KEY', // 替换为您的Infura Key
    tag: 'testnet', 
    color: 'orange',
    blockExplorer: 'https://goerli.etherscan.io'
  },
  mumbai: {
    name: 'Polygon Mumbai',
    chainId: 80001,
    rpcUrl: 'https://polygon-mumbai.infura.io/v3/YOUR_INFURA_KEY', // 替换为您的Infura Key
    tag: 'testnet',
    color: 'purple',
    blockExplorer: 'https://mumbai.polygonscan.com'
  },
  mainnet: {
    name: 'Ethereum Mainnet',
    chainId: 1,
    rpcUrl: 'https://mainnet.infura.io/v3/YOUR_INFURA_KEY', // 替换为您的Infura Key
    tag: 'mainnet',
    color: 'red',
    blockExplorer: 'https://etherscan.io'
  },
  polygon: {
    name: 'Polygon Mainnet',
    chainId: 137,
    rpcUrl: 'https://polygon-mainnet.infura.io/v3/YOUR_INFURA_KEY', // 替换为您的Infura Key
    tag: 'mainnet',
    color: 'purple',
    blockExplorer: 'https://polygonscan.com'
  }
};

// 合约地址配置
// ⚠️ 重要：部署后请及时更新以下地址
export const CONTRACT_ADDRESSES = {
  hardhat: {
    // 本地Hardhat网络地址 (每次重启会变化)
    BOOK1155: '0x5FbDB2315678afecb367f032d93F642f64180aa3', // 示例地址
    TokenMarketplace: '0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512',
    TraceBook: '0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0',
    BookIPFSManager: '0xCf7Ed3AccA5a467e9e704C703E8D87F634fB0Fc9',
    TokenCodeVerify: '0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9'
  },
  sepolia: {
    // Sepolia测试网地址 (部署后填写)
    BOOK1155: '', // 待填写
    TokenMarketplace: '',
    TraceBook: '',
    BookIPFSManager: '',
    TokenCodeVerify: ''
  },
  goerli: {
    // Goerli测试网地址 (部署后填写)
    BOOK1155: '', // 待填写
    TokenMarketplace: '',
    TraceBook: '',
    BookIPFSManager: '',
    TokenCodeVerify: ''
  },
  mumbai: {
    // Polygon Mumbai测试网地址 (部署后填写)
    BOOK1155: '', // 待填写
    TokenMarketplace: '',
    TraceBook: '',
    BookIPFSManager: '',
    TokenCodeVerify: ''
  },
  mainnet: {
    // 以太坊主网地址 (正式部署后填写)
    BOOK1155: '', // 待填写
    TokenMarketplace: '',
    TraceBook: '',
    BookIPFSManager: '',
    TokenCodeVerify: ''
  },
  polygon: {
    // Polygon主网地址 (正式部署后填写)
    BOOK1155: '', // 待填写
    TokenMarketplace: '',
    TraceBook: '',
    BookIPFSManager: '',
    TokenCodeVerify: ''
  }
};

// 智能合约ABI
export const BOOK1155_ABI = [
  "function getAllMintedBookIds() view returns (uint256[])",
  "function getNormalTokenInfo(uint256 id) view returns (tuple(string ISBN, string ownProve, uint256 mintTimestamp, bytes32 MetadataHash, string imageURI, uint256 price, address minter, string rightProve))",
  "function mint(uint256 id, string ISBN, uint256 amount, string imageURI, uint256 price, string contentHash, string metadataHash, string previewHash, bool isEncrypted, string encryptionKey)",
  "function getIdAmount(uint256 id) view returns (uint256)",
  "function getIdSaleAmount(uint256 id) view returns (uint256)",
  "function balanceOf(address account, uint256 id) view returns (uint256)",
  "function setApprovalForAll(address operator, bool approved)",
  "event BookMinted(uint256 indexed id, string ISBN, uint256 amount, string imageURI, uint256 price, address indexed minter, uint256 timestamp)",
  "event BookListingChanged(uint256 indexed id, bool isListed, uint256 saleAmount, address indexed operator)"
];

export const MARKETPLACE_ABI = [
  "function createListing(address tokenContract, uint256 tokenId, uint256 amount)",
  "function executePurchase(uint256 listingId, uint256 amount) payable",
  "function getAllActiveListings() view returns (tuple(uint256 listingId, address seller, address tokenContract, uint256 tokenId, uint256 amount, uint256 price, string ISBN, bool isActive)[])",
  "function getOwnInformation(address owner) view returns (tuple(string tokenCode, string ISBN, uint256 price, uint256 purchaseTime)[])"
];

// 获取网络配置
export const getNetworkConfig = (networkKey) => {
  return NETWORKS[networkKey];
};

// 获取合约地址
export const getContractAddresses = (networkKey) => {
  return CONTRACT_ADDRESSES[networkKey];
};

// 检查网络是否配置完整
export const isNetworkConfigured = (networkKey) => {
  const addresses = CONTRACT_ADDRESSES[networkKey];
  return addresses && addresses.BOOK1155 && addresses.BOOK1155 !== '';
};

// 获取区块浏览器链接
export const getExplorerLink = (networkKey, hash, type = 'tx') => {
  const network = NETWORKS[networkKey];
  if (!network || !network.blockExplorer) return '#';
  
  switch (type) {
    case 'tx':
      return `${network.blockExplorer}/tx/${hash}`;
    case 'address':
      return `${network.blockExplorer}/address/${hash}`;
    case 'token':
      return `${network.blockExplorer}/token/${hash}`;
    default:
      return network.blockExplorer;
  }
};

// 部署指南
export const DEPLOYMENT_GUIDE = {
  steps: [
    {
      title: '1. 启动本地Hardhat网络',
      command: 'npx hardhat node',
      description: '在项目根目录运行此命令启动本地区块链'
    },
    {
      title: '2. 部署合约到本地网络',
      command: 'npx hardhat run scripts/deploy.js --network localhost',
      description: '部署所有合约到本地网络'
    },
    {
      title: '3. 更新合约地址',
      description: '将部署后的合约地址复制到 CONTRACT_ADDRESSES.hardhat 中'
    },
    {
      title: '4. 部署到测试网',
      command: 'npx hardhat run scripts/deploy.js --network sepolia',
      description: '部署到Sepolia测试网（需要先配置网络和私钥）'
    }
  ]
}; 